<?php

namespace Home\Controller; //当前命名空间跟文件目录一样
use Common\Common\CommonController; //引用Common目录下的Common下的CommonController
class UserController extends CommonController
{

	//会员注册功能
	public function register()
	{
		$error = array();
		//判断是否有POST表单提交
		if (IS_POST) {
			//获取表单传递的post参数列表
			$user_info = I('post.');
			//获取User模型类对象
			$model = D('User');
			//进行注册信息检查
			$error = $model->validation($user_info);
			//没有格式错误信息
			if (empty(implode($error))) {
				//进行用户名存在判断，并插入注册数据
				$error[] = $model->register($user_info);
				if (empty(implode($error))) {
					$this->success("恭喜您，注册成功！！！", U('?m=Home&c=user&a=login'));
					die();
				}
			}
		}
		$this->assign("error", $error);
		$this->display();
	}

	//会员登录功能
	public function login()
	{
		//判断是否有POST表单提交
		if (IS_POST) {
			//获取表单传递的post参数列表
			$user_info = I('post.');
			//获取User模型类对象
			$model = D('User');
			//进行登录信息检查
			$error = $model->checkLogin($user_info);
			if (empty($error)) {
				//保留客户会话信息
				session('u_name', $user_info['u_name']);
				//判断用户是否勾选记住我
				if ($user_info['auto_login'] == "on") {
					//用cookie保存用户名和密码，方便下次客户直接登录 
					cookie('u_name', $user_info['u_name'], 3600);
					cookie('u_pwd', $user_info['u_pwd'], 3600);
				} else {
					//清除cookie
					cookie('u_name', null);
					cookie('u_pwd', null);
				}
				$this->success("用户登录成功，正在登陆！！！", U('?m=Home&c=Goods&a=index'));
				die;
			}
		}
		$address = COMMON_PATH . 'Common';
		$this->assign('u_name', cookie('u_name'));  //传递cookie值，为复选框选择
		$this->assign('error', $error);    //传递错误信息
		$this->assign('address', $address); //传递验证码所在目录地址
		$this->display();
	}

	//会员信息展示功能
	public function showList()
	{
		//调用公共控制器中的检查用户身份
		$this->checkUser();
		//获取User模型类对象
		$model = D('User');
		//获取传递的session信息session('u_name'),通过会话信息查询用户个人信息
		$where = array('u_name' => session('u_name'));
		$field = 'user_id,u_name,u_gender,mobile_phone,u_email,u_address,u_birthday'; //获取字段字符串
		$user_info = $model->field($field)->where($where)->find();
		$this->assign('user_info', $user_info);
		$this->display();
	}

	//会员信息修改功能
	public function update()
	{
		//调用公共控制器中的检查用户身份
		$this->checkUser();
		//获取User模型类对象
		$model = D('User');
		if (IS_POST) //有表单提交
		{
			//create()接收表单数据
			$user_info = $model->create();
			//进行修改信息检查
			$error[] = checkPhone($user_info['mobile_phone']);
			$error[] = checkEmail($user_info['u_email']);
			//没有格式错误信息
			if (empty(implode($error))) {
				//进行数据更新，失败提示错误并放回个人信息列表
				if ($model->where("u_name='" . session('u_name') . "'")->save($user_info) !== false) {
					$this->success("恭喜您，修改成功！！！", U('?m=Home&c=user&a=showList'));
					die;
				}
				$this->error("出现未知错误！！！", U('?m=Home&c=user&a=showList'));
				die;
			}
			$this->error("手机或邮箱格式有误，修改失败！！！", U('?m=Home&c=user&a=showList'));
			die;
		}
		//获取传递的session信息session('u_name'),通过会话信息查询用户个人信息
		$where = array('u_name' => session('u_name'));
		$field = 'u_gender,mobile_phone,u_email,u_address,u_birthday'; //获取字段字符串
		$user_info = $model->field($field)->where($where)->find();
		$this->assign('user_info', $user_info);
		$this->display();
	}

	//会员安全退出
	public function loginout()
	{
		session(null);
		$this->success("退出成功", U("?m=Home&c=user&a=login"));
	}
}
